<?php
session_start();
if (isset($_POST['id']) && isset($_SESSION['openid'])){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
    $openid=$_SESSION['openid'];
    
    if ('oE4KPwc-PM8zroeef52R0YjkDotA' != $openid) {
      $jiId=0;
      echo json_encode(array('s'=>'ok','hc'=>'nochou'));
      exit;
    }

    $xieXie=array(2,4);
    shuffle($xieXie);
    
    $haschou='no';
    $query=$mysqli->query("SELECT id,haschou from `uinfo` where openid='".$openid."' limit 0,1 ");
    $hasThisMan=true;
    if($query->num_rows>0){
        $getU=$query->fetch_assoc();
        if (1 == $getU['haschou']){
            $datar=$xieXie[0];
            $haschou='y';
            $jiId=0;
            echo json_encode(array('s'=>'ok','hc'=>$haschou));
            exit;
        }
    }else{
        $hasThisMan=false;
        
    }
    
    
  
    
    $mysqli->query("insert into chou_log(openid,createtime) values('".$openid."','".date("Y-m-d H:i:s")."') ");
    
    $query=$mysqli->query("SELECT id from `chou_log` where openid='".$openid."' order by id desc limit 0,1 ");
    $getLog=$query->fetch_assoc();
    $logId=$getLog['id'];
    $query=$mysqli->query("SELECT ji from `chi` where cid='".$logId."' limit 0,1 ");
    $jiId=0;
    if($query->num_rows>0){
        $getChi=$query->fetch_assoc();
        $jiId=$getChi['ji'];
        
        if(false == $hasThisMan){
            $jiId=0;
        }
        if (3 == $jiId){
            $rand=rand(1,2);
            if(1 == $rand){
               // $datar=3;
            }else{
               // $datar=7;
            }
            $mysqli->query("insert into chou(openid,cid,createtime) values('".$openid."',3,'".date("Y-m-d H:i:s")."') ");
        }
        if (2 == $jiId){
            //$datar=5;
            //$mysqli->query("insert into chou(openid,cid,createtime) values('".$openid."',2,'".date("Y-m-d H:i:s")."') ");
        }
        if (1 == $jiId){
            //$datar=1;
            //$mysqli->query("insert into chou(openid,cid,createtime) values('".$openid."',1,'".date("Y-m-d H:i:s")."') ");
        }
    }
    
    
    $p1='';

    if(1 == $datar){
        //$p1='p3-ajz.png';
    }
    if(5 == $datar){
        //$p1='p3-bjy.png';
    }
    if(3 == $datar || 7 == $datar){
        //$p1='p3-cjf.png';
    }
    
    $mysqli->query("update `uinfo` set haschou=1 where openid='".$openid."' ");
    
    
    $mysqli->close();
    echo json_encode(array('s'=>'ok','n'=>0,'r'=>$datar,'q'=>0,'p1'=>$p1,'hc'=>$haschou));
}
